import java.util.Arrays;

/**
 * @Author 孤风雪影
 * @Email gitee.com/efairy520
 * @Date 2024/4/16 23:38
 * @Version 1.0
 */
public class leecode_128 {
    /**
     * 给定一个未排序的整数数组 nums ，找出数字连续的最长序列（不要求序列元素在原数组中连续）的长度。
     * 输入：nums = [100,4,200,1,3,2]
     * 输出：4
     * @param args
     */
    public static void main(String[] args) {
        int[] nums = {1,2,0,1};
        leecode_128 leecode128 = new leecode_128();
        int i = leecode128.longestConsecutive(nums);
        System.out.println("i = " + i);
    }

    public int longestConsecutive(int[] nums) {
        Arrays.sort(nums);
        if (nums.length == 0) return 0;
        int longestSegment = 1;
        int result = 1;
        for (int i = 0; i < nums.length - 1; i++) {
            if (nums[i + 1] - nums[i] == 1) {
                longestSegment++;
            } else if (nums[i + 1] - nums[i] == 0) {

            } else {
                longestSegment = 1;
            }
            result = Math.max(result, longestSegment);
        }
        return result;
    }
}
